/*
 * Copyright (c) 2012 Robert Burrell Donkin robertburrelldonkin.name
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software 
 * is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 */
package sekhmet.audio.source.mic;

import java.util.Comparator;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.AudioFormat.Encoding;

public class AudioSelectors {

	private final static Comparator<AudioFormat> CHANNELS = new Comparator<AudioFormat>() {

		public int compare(AudioFormat audio1, AudioFormat audio2) {
			return audio1.getChannels() - audio2.getChannels();
		}
		
	};
	
	private final static Comparator<AudioFormat> PCM_UNSIGNED = new Comparator<AudioFormat>() {

		public int compare(AudioFormat audio1, AudioFormat audio2) {
			final Encoding encoding1 = audio1.getEncoding();
			final Encoding encoding2 = audio2.getEncoding();
			if (encoding1 == encoding2) return 0;
			if (encoding1 == Encoding.PCM_UNSIGNED) {
				return -1;
			} else {
				return 1;
			}
		}
		
	};

	private final static Comparator<AudioFormat> FRAME_RATE = new Comparator<AudioFormat>() {

		public int compare(AudioFormat audio1, AudioFormat audio2) {
			if (audio1.getFrameRate() == audio2.getFrameRate()) return 0;
			if (audio1.getFrameRate() == AudioSystem.NOT_SPECIFIED) return 1;
			return audio1.getFrameRate() > audio2.getFrameRate() ? -1 : 1;
		}
		
	};


	private final static Comparator<AudioFormat> SAMPLE_RATE = new Comparator<AudioFormat>() {

		public int compare(AudioFormat audio1, AudioFormat audio2) {
			if (audio1.getSampleRate() == audio2.getSampleRate()) return 0;
			if (audio1.getSampleRate() == AudioSystem.NOT_SPECIFIED) return 1;
			return audio1.getSampleRate() > audio2.getSampleRate() ? -1 : 1;
		}
		
	};
	
	private final static Comparator<AudioFormat> HIGH_MONO_PCM_UNSIGNED = new Comparator<AudioFormat>() {

		public int compare(AudioFormat audio1, AudioFormat audio2) {
			final int channels = CHANNELS.compare(audio1, audio2);
			if (channels == 0) {
				final int format = PCM_UNSIGNED.compare(audio1, audio2);
				if (format == 0) {
					return SAMPLE_RATE.compare(audio1, audio2);
				}
				return format;
			}
			return channels;
		}
		
	};
	
	public static Comparator<AudioFormat> channels() {
		return CHANNELS;
	}
	
	public static Comparator<AudioFormat> pcmUnsigned() {
		return PCM_UNSIGNED;
	}
	
	public static Comparator<AudioFormat> frameRate() {
		return FRAME_RATE;
	}	

	
	public static Comparator<AudioFormat> sampleRate() {
		return SAMPLE_RATE;
	}	

	
	public static Comparator<AudioFormat> highMonoPCM() {
		return HIGH_MONO_PCM_UNSIGNED;
	}
}
